package com.ocnyang.qbox1.app1.module.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ocnyang.qbox1.app1.R;
import com.ocnyang.qbox1.app1.base.BaseCommonActivity;
import com.ocnyang.qbox1.app1.config.Const;
import com.ocnyang.qbox1.app1.db.service.UserService;
import com.ocnyang.qbox1.app1.module.mains.MainsActivity;
import com.ocnyang.qbox1.app1.module.start.SplashActivity;
import com.ocnyang.qbox1.app1.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseCommonActivity implements CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.iv_common_close)
    ImageView ivCommonClose;
    @BindView(R.id.tv_account_login)
    TextView tvAccountLogin;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.pass_et)
    EditText passEt;
    @BindView(R.id.parent_psd)
    RelativeLayout parentPsd;
    @BindView(R.id.button)
    TextView button;
    @BindView(R.id.tv_register)
    TextView tvRegister;

    UserService uService;


    @Override
    public void initContentView() {
        setContentView(R.layout.activity_login);

    }

    @Override
    public void initView() {
        uService = new UserService(LoginActivity.this);
        initSwitch();

    }


    private void initSwitch() {
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initPresenter() {

    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_common_close, R.id.button, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_common_close:
                finish();
                break;
            case R.id.button:
                String name = phoneEt.getText().toString();
                String pass = passEt.getText().toString();
                Log.i("TAG", name + "_" + pass);
                if (TextUtils.isEmpty(name)){
                    Toast.makeText(LoginActivity.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(pass)){
                    Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (name.equals("15107031683")&&pass.equals("123456")){
                    Log.i("TAG", "登录成功");
                    SPUtils.put(LoginActivity.this,Const.TOKEN,"已登录");
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(LoginActivity.this, MainsActivity.class));
                    finish();
                }else {
                    Log.i("TAG", "登录失败");
                    Toast.makeText(LoginActivity.this, "账号或密码不正确", Toast.LENGTH_LONG).show();
                }
//                boolean flag = uService.login(name, pass);
//                if (flag) {
//                    Log.i("TAG", "登录成功");
//                    SPUtils.put(LoginActivity.this,Const.TOKEN,"已登录");
//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
//                    startActivity(new Intent(LoginActivity.this, MainsActivity.class));
//                    finish();
//                } else {
//                    Log.i("TAG", "登录失败");
//                    Toast.makeText(LoginActivity.this, "账号或密码不正确", Toast.LENGTH_LONG).show();
//                }
                break;
            case R.id.tv_register:
                startActivity(new Intent(LoginActivity.this,RegistActivity.class));
                break;
        }
    }
}
